﻿Imports DTO
Imports DAL

Public Class FornecedorBLL

#Region "Métodos"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Sub Incluir(fornecedor As FornecedorDTO)
        Dim dalFornecedor As New FornecedorDAL
        Dim dtoFornecedor As New FornecedorDTO
        Try
            If ValidaCNPJ(fornecedor.pCnpj) Then
                dtoFornecedor = dalFornecedor.Consultar(fornecedor.pCnpj)
                If IsNothing(dtoFornecedor) Then
                    dalFornecedor.Incluir(fornecedor)
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="fornecedor"></param>
    ''' <remarks></remarks>
    Sub Alterar(fornecedor As FornecedorDTO)
        Dim dalFornecedor As New FornecedorDAL

        Try
            dalFornecedor.Alterar(fornecedor)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="cnpj"></param>
    ''' <remarks></remarks>
    Sub Excluir(cnpj As String)
        Dim dalFornecedor As New FornecedorDAL
        Try
            dalFornecedor.Excluir(cnpj)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="cnpj"></param>
    ''' <remarks></remarks>
    Sub Consultar(cnpj As String)
        Dim dalFornecedor As New FornecedorDAL
        Try
            dalFornecedor.Consultar(cnpj)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function Listar() As List(Of FornecedorDTO)
        Dim dtoFornecedor As List(Of FornecedorDTO)
        Dim dalFornecedor As New FornecedorDAL

        Try

            dtoFornecedor = dalFornecedor.Listar()
            Return dtoFornecedor
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Private Function ValidaCNPJ(ByVal value As String) As Boolean
        Dim multiplier1() As Integer = New Integer() {5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
        Dim multiplier2() As Integer = New Integer() {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2}
        Dim sum As Integer = 0
        Dim leftOver As Integer
        Dim digit As String
        value = value.Trim()
        value = value.Replace(".", "")
        value = value.Replace("/", "")
        value = value.Replace("-", "")
        Dim tempCPF As String = value
        If Not value.Length = 14 Then
            Return False ' Maior/Menor que 14 dígitos
        End If
        tempCPF = tempCPF.Substring(0, 12)
        For i As Integer = 0 To 11
            sum = sum + Integer.Parse(tempCPF(i).ToString * multiplier1(i))
        Next
        leftOver = sum Mod 11
        If leftOver < 2 Then
            leftOver = 0
        Else
            leftOver = 11 - leftOver
        End If
        digit = leftOver.ToString
        tempCPF = tempCPF + digit
        sum = 0
        For i As Integer = 0 To 12
            sum = sum + Integer.Parse(tempCPF(i).ToString * multiplier2(i))
        Next
        leftOver = sum Mod 11
        If leftOver < 2 Then
            leftOver = 0
        Else
            leftOver = 11 - leftOver
        End If
        digit = digit + leftOver.ToString
        Return value.EndsWith(digit)

    End Function


#End Region

End Class
